//
//  KKPickerView.m
//  F2FClient
//
//  Created by xujingjing on 16/6/15.
//  Copyright © 2016年 xujingjing. All rights reserved.
//

#import "KKPickerView.h"
#import "AppDelegate.h"
#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height

@interface KKPickerView()<UIPickerViewDataSource,UIPickerViewDelegate>{

    NSArray *dataSource;
    UIView *whithView;
    UIView *contantView;
    void(^backBlock)(NSString *);
    NSString *returnString;
    NSInteger column;
    NSArray *column_two_datasource;
   
    UIPickerView  *pickerView;
    UIDatePicker *datepicker;
    NSInteger  loadtype;
    NSString *twoPartfirst;//类型3 拼接时间
    NSString *twoParttwo;//类型3 拼接事件

}

@end

@implementation KKPickerView

-(instancetype)initWIthType:(NSInteger)type withDatasource:(NSArray *)source response:(void(^)(NSString *))block columns:(NSInteger)columns{
    if (self = [super init]) {
        column = columns;
        loadtype = type;
        self.frame = [UIScreen mainScreen].bounds;
        [self setViewInterface];

        if (block) {
            backBlock = block;
        }
        
        if (source) {
            
            dataSource = [source copy];
            
        }
        
        if (type==0) {
            
            [self pickerView];
        }else if(type == 1){//年与日 时分秒
        
            [self datepicker:0];
        }else if(type == 2){ //年月日
        
            [self datepicker:1];
        
        }else if (type == 3){
        
            [self loadTimeAndText];
        }
        
     
        
    }
    return self;



}
-(void)loadTimeAndText{
    
    [self pickerView];
    pickerView.frame = CGRectMake(kWindowWidth*4/5-20, 40, kWindowWidth*1/5+20, 260);
    
    [self datepicker:1];
    datepicker.frame = CGRectMake(0, 40, kWindowWidth*4/5-20, 260);
    

}
#pragma mark View setting

-(void)setViewInterface{
    contantView = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT, WIDTH, 300)];
    [self addSubview:contantView];
    
    
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];//set black-background between  0-1
    
    //View
   whithView  = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 40)];
    whithView.backgroundColor = [UIColor whiteColor];
    [contantView addSubview:whithView];
    
    //button
    for (int i=0; i<2; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake((WIDTH-60)*i, 0, 60, 40);
        
        [button setTitle:i==0 ? @"取消":@"确定" forState:UIControlStateNormal];
       
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        button.tag = i+10;
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [whithView addSubview:button];
        
        
        
    }
    
    
    
}
#pragma mark buttonPressed
-(void)buttonPressed:(UIButton *)sender{
    if (sender.tag ==10) {
        
        
    }else{
       
        backBlock(returnString);
        
        if (loadtype == 3) {
            NSString *str = [NSString stringWithFormat:@"%@  %@",twoPartfirst,twoParttwo];
            backBlock(str);
        }
        
    }
   [self dismiss];


}
#pragma mark datePicker setting
-(void)datepicker:(NSInteger)num{
    datepicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 40, WIDTH, 260)];
    datepicker.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [contantView addSubview:datepicker];
    //first-Value  (havent't be slide)
    if (num ==0) {
         [datepicker addTarget:self action:@selector(dataPickerSelest:number:) forControlEvents:UIControlEventValueChanged];
        returnString = [self changeTOStringWithDate:[NSDate date] ];
        twoPartfirst = returnString;
    }else{
    [datepicker addTarget:self action:@selector(ymd_dataPickerSelest:number:) forControlEvents:UIControlEventValueChanged];
        datepicker.datePickerMode = UIDatePickerModeDate;
     returnString = [self ymd_changeTOStringWithDate:[NSDate date] ];
     twoPartfirst = returnString;

    }
    
}


#pragma  mark pickerView setting
-(void)pickerView{
    for (id obj in dataSource) {
        if ([obj isKindOfClass:[NSDictionary class]]) {
            column_two_datasource = [dataSource[0] allValues][0];
        }
        break;//beause we know dataSource all are NSDictionary and just two state
    }
//性别 默认：男
   // returnString = column_two_datasource[0];
     returnString = dataSource[0];
    twoParttwo = returnString;

    pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, WIDTH, 260)];
    
    pickerView.delegate = self;
    pickerView.dataSource = self;
    pickerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [contantView addSubview:pickerView];


}

#pragma mark UIDatePicker  ValueChanged

-(void)dataPickerSelest:(UIDatePicker *)datePicker number:(NSInteger)num{
        returnString= [self changeTOStringWithDate:datePicker.date];
   
    twoPartfirst = returnString;
}
-(void)ymd_dataPickerSelest:(UIDatePicker *)datePicker number:(NSInteger)num{
       returnString= [self ymd_changeTOStringWithDate:datePicker.date];
    
    twoPartfirst = returnString;
}

//date->string
-(NSString *)changeTOStringWithDate:(NSDate *)date1{
  
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
   [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
   
   return [formatter stringFromDate:date1];
   
}
-(NSString *)ymd_changeTOStringWithDate:(NSDate *)date1{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:@"yyyy-MM-dd"];
   
    return [formatter stringFromDate:date1];
    
}

#pragma mark show
-(void)show{
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    [app.window addSubview:self];
    
   // 如果导航栏存在，想要在mainVC 中添加show的view,可以直接用上面的方法，不需要在mainVC 中addSubview  .同时黑色半透明view可以遮住导航栏。
    
[UIView animateWithDuration:0.4 animations:^{
    contantView.center = CGPointMake(WIDTH/2, contantView.center.y-contantView.frame.size.height);
    
   
}];

}

-(void)dismiss{
    
[UIView animateWithDuration:0.4 animations:^{
    contantView.center = CGPointMake(WIDTH/2, contantView.center.y+contantView.frame.size.height);
    
}completion:^(BOOL finished) {
    [self removeFromSuperview];
}];
    

}

#pragma mark UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return column;

}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component==0) {
         return dataSource.count;
    }else{
    
        return column_two_datasource.count;
    }
   
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component==0) {
        if (column_two_datasource) {
            return [dataSource[row] allKeys][0];
        }else{
            
         return dataSource[row];
        
        }
       
    }
    return column_two_datasource[row];
    
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        
        if (column_two_datasource) {
            column_two_datasource  = [dataSource[row] allValues][0];
            
            returnString = column_two_datasource[0];//first be select  ,second not be select,
            twoPartfirst = returnString;

            [pickerView reloadComponent:1];
        }
        else{
        returnString = dataSource[row];
            twoParttwo = returnString;
        }
       
    }
    else{
        returnString = column_two_datasource[row];
        twoParttwo = returnString;
    }
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{
    
    
    UILabel *myView = nil;
    
    if (component == 0) {
        
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)];
        
        myView.textAlignment = NSTextAlignmentCenter;
        
        myView.text = [dataSource objectAtIndex:row];
        
        if (loadtype == 3) {
            
            myView.font = [UIFont systemFontOfSize:12];         //用label来设置字体大小

        }else{
            myView.font = [UIFont systemFontOfSize:14];         //用label来设置字体大小

        }
        
        myView.backgroundColor = [UIColor clearColor];
        
    }else {
        
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 180, 30)];
        
        myView.text = [dataSource objectAtIndex:row];
        
        myView.textAlignment = NSTextAlignmentCenter;
        
        myView.font = [UIFont systemFontOfSize:14];
        
        myView.backgroundColor = [UIColor clearColor];
        
    }
    return myView;
    
}
@end
